<?php
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
// use App\Models\Certificate;
// use App\Models\Member;
use Illuminate\Support\Facades\Http;
use Auth;
use LaravelJsTan\Facades\JsTan;
use JsTan\Client;
use JsTan\Route;
use JsTanInIt\JsTanInIt;
use Log;
use App\Services\JsTan\Ecshop;

class JsTanController extends Controller
{

    /**
     * @api {post} /jstan/test
     * 聚水潭测试
     */
    public function test(Request $request)
    {
        // $authUrl = JsTan::Client()->createAuthUrl();
        // $response = JsTan::Client()->request(
        //     Route::QUERY_SHOPS,[
        //         'page_index' => 1,
        //         'page_size' => 10
        //     ]
        // );
        // dd($authUrl, $response);

        $client = Client::getInstance(config('jsTan'));
        $route = Route::getInstance();


        // $response = $client->getAccessToken('001');

        // 获取店铺列表
        // $response = JsTan::Client()->request(
        //     Route::QUERY_SHOPS,[
        //         'page_index' => 1,
        //         'page_size' => 100
        //     ]
        // );
        // $response = json_decode($response, 1);
        // dd(config('jsTan'),$response);

      //    18 => array:13 [
      //   "group_name" => null
      //   "created" => "2024-11-22 21:40:26.000"
      //   "co_id" => 13659696
      //   "session_uid" => null
      //   "shop_site" => "商家自有商城"
      //   "shop_name" => "ecshop商城"
      //   "nick" => "self_5428843181582116987"
      //   "shop_id" => 17247322
      //   "session_expired" => "------永久授权------"
      //   "group_id" => 0
      //   "shop_url" => null
      //   "organization" => null
      //   "short_name" => ""
      // ]
        // dd(config('jsTan'),$response);

        // 店铺资料上传
        $shop_items = [
            'items' => [
                0=> [
                    "sku_id" => "0708062",
                    "i_id" => "1SFCC0708062",
                    "shop_sku_id" => "1038",
                    'sku_code' => '1038',
                    'shop_i_id' => '1SFCC0708062',
                    'shop_sku_id' => '0708062',
                    "original_sku_id" => "1038",
                    "name" => "PM Fitline 青柠小红",
                    "shop_properties_value" => "1罐;顺丰直邮CC",
                    "sku_sign" => "0708062",
                    "shop_id" => 17292311,
                    "shop_sku_url" => "https://www.duesselpharm.com/goods.php?id=1038",
                ],
            ],
        ];
        // $response = JsTan::Client()->request(
        //     Route::UPLOAD_SKUMAP, $shop_items
        // );
        // $response = json_decode($response, 1);
        // dd(config('jsTan'),$response);

        //普通商品资料上传
        $goods_items = [
            'items' => [
                0 => [
                    'sku_id' => '0708062',
                    'i_id' => '1SFCC0708062',
                    'brand' => 'PM产品',
                    's_price' => '254',
                    'pic' => 'https://www.duesselpharm.com/images/202410/goods_img/1038_G_1727875083991.jpg',
                    'pic_big' => 'https://www.duesselpharm.com/images/202410/goods_img/1038_G_1727875083991.jpg',
                    'sku_pic' => 'https://www.duesselpharm.com/images/202410/thumb_img/1038_thumb_P_1727875083541.jpg',
                    'name' => 'PM Fitline 青柠小红',
                    'properties_value' => '1罐;顺丰直邮CC',
                    'weight' => '0.175',
                    'enabled' => 1,
                    'sku_code' => '1038',
                    'market_price' => '254',
                ],
            ],
        ];
        // $response = JsTan::Client()->request(
        //     Route::UPLOAD_ITEMSKU, $goods_items
        // );
        // $response = json_decode($response, 1);
        // dd(config('jsTan'),$response);
        // array:3 [ // app/Http/Controllers/Api/JsTanController.php:111
        //   "msg" => "执行成功"
        //   "code" => 0
        //   "data" => array:1 [
        //     "datas" => array:1 [
        //       0 => array:3 [
        //         "msg" => "上传/更新成功"
        //         "sku_id" => "0708062"
        //         "is_success" => true
        //       ]
        //     ]
        //   ]
        // ]

        // 订单上传
        $order_params = [
            'shop_id' => 17292311,
            'so_id' => '20241129561631234',
            'order_date' => '2024-11-28 12:12:12',
            'shop_status' => 'WAIT_SELLER_SEND_GOODS',
            'shop_buyer_id' => 'lu_jiu',
            'receiver_state' => '上海',
            'receiver_city' => '上海市',
            'receiver_district' => '徐汇区',
            'receiver_address' => '王家厍路55弄',
            'receiver_name' => '路久',
            'receiver_phone' => '13564137019',
            'receiver_mobile' => '13564137019',
            'receiver_country' => 'CN',
            // 'receiver_zip' => 200211,
            'pay_amount' => 105.04,
            'freight' => 0.00,
            'buyer_message' => '买家留言',
            'remark' => '卖家备注',
            'items' => [
                0 => [
                    'sku_id' => '0708062',
                    'shop_sku_id' => '0708062',
                    'shop_i_id' => '1SFCC0708062',
                    'amount' => 105.04,
                    'base_price' => 105.04,
                    'qty' => 1,
                    'name' => 'PM Fitline 青柠小红',
                    'outer_oi_id' => '1038',

                ],

            ],

            'pay' => [
                'outer_pay_id' => '155775985878733471',
                'pay_date' => '2024-11-28 12:12:12',
                'payment' => '线下',
                'seller_account' => '22',
                'buyer_account' => '22',
                'amount' => 105.04,
            ],
            'card' => [
                'name' => '张三',
                'card_id' => '412823199010102121',
            ],
        ];
        $response = JsTan::Client()->request(
            Route::UPLOAD_ORDERS, [$order_params]
        );
        $response = json_decode($response, 1);
        dd(config('jsTan'),$response);
        // array:3 [ // app/Http/Controllers/Api/JsTanController.php:177
        //   "msg" => "执行成功"
        //   "code" => 0
        //   "data" => array:2 [
        //     "datas" => array:1 [
        //       0 => array:5 [
        //         "msg" => "成功"
        //         "issuccess" => true
        //         "so_id" => "20241129561631234"
        //         "o_id" => 163877
        //         "split_id" => null
        //       ]
        //     ]
        //     "requestId" => null
        //   ]
        // ]

        /**
        * 刷新access_token （请求示例）
        */
        // $response = $client->refreshToken('001');

    }


    /**
     * @api {post} /jstan/upload_skumap
     * 店铺商品资料上传
     */
    public function upload_skumap(Request $request)
    {
        $request = $request->all();

        $shop_items = json_decode($request['shop_items'], 1);

        $response = JsTan::Client()->request(
            Route::UPLOAD_SKUMAP, $shop_items
        );
        $response = json_decode($response, 1);
        // dd(config('jsTan'),$response);

        return $response;
    }

    /**
     * @api {post} /jstan/upload_itemsku
     * 普通商品资料上传
     */
    public function upload_itemsku(Request $request)
    {
        $request = $request->all();

        $goods_items = json_decode($request['goods_items'], 1);

        $response = JsTan::Client()->request(
            Route::UPLOAD_ITEMSKU, $goods_items
        );
        $response = json_decode($response, 1);
        // dd(config('jsTan'),$response);

        return $response;
    }

    /**
     * @api {post} /jstan/upload_orders
     * 订单上传
     */
    public function upload_orders(Request $request)
    {
        $request = $request->all();

        $order_params = json_decode($request['order_params'], 1);

        $response = JsTan::Client()->request(
            Route::UPLOAD_ORDERS, $order_params
        );
        $response = json_decode($response, 1);
        // dd(config('jsTan'),$response);

        return $response;
    }


    /**
     * @api {post} /shop/order_delivery
     * 接收:订单发货
     */
    public function order_delivery(Request $request)
    {
        $request = $request->all();

        //{"o_id":1,"l_id":"test","so_id":"test","lc_id":"test","order_from":"test","wms_co_id":0,"logistics_company":"test","send_date":"2024-12-08 23:46:01.687","items":[{"oi_id":1,"sku_id":"test","qty":1,"name":"test","outer_oi_id":"test","so_id":"test"}],"ts":"1733672761","partnerid":"erp","method":"logistics.upload","sign":"4df98d6c1534e6d2b644cfb0478e6b89"}

        // {"o_id":167859,"l_id":"DEUHYD400066038247EU","so_id":"2023022852799","lc_id":"\u8d27\u6613\u8fbe__12","order_from":"open","wms_co_id":13659696,"logistics_company":"\u4e5d\u8058","send_date":"2024-12-08 23:52:37.019",items":[],"ts":"1733673157","partnerid":"erp","method":"logistics.upload","sign":"ab007bad85ab2107cb5f1ec83d2cf9e9"}

        Log::info('order_delivery_request:'. json_encode($request));

        $msg = '';
        $res = Ecshop::orderDelivery($request, $msg);
        // dd('sss',$res, $msg);
        if (!$res)
        {
            $result = ['code' => 500, 'msg' => $msg];
        }
        else
        {
            $result = ['code' => 0, 'msg' => '执行成功'];
        }

        return json_encode($result);
    }

    /**
     * @api {get} /jstan/order_cancel
     * 接收：订单取消
     */
    public function order_cancel(Request $request)
    {
        $request = $request->all();

        Log::info('order_cancel_request:'. json_encode($request));

        // $request = '{"so_id":"2024120866708","remark":"test","ts":"1733672762","partnerid":"erp","method":"cancel.order","sign":"aaad138d970f219d76bfdba9f1608dc0"}';

        // $request = json_decode($request, 1);

        $msg = '';
        $res = Ecshop::orderCancel($request, $msg);
        // dd('sss',$res, $msg);
        if (!$res)
        {
            $result = ['code' => 500, 'msg' => $msg];
        }
        else
        {
            $result = ['code' => 0, 'msg' => '执行成功'];
        }

        return json_encode($result);
    }

    /**
     * @api {get} /shop/update_inventory
     * 接收：更新库存
     */
    public function update_inventory(Request $request)
    {
        $request = $request->all();

        Log::info('update_inventory_request:'. json_encode($request));

        // $request = '{"datas":[{"i_id":null,"id":0,"qty":100,"shop_i_id":"test","shop_id":0,"shop_sku_id":"test","sku_id":"sandancesensitiv2","sku_source":null,"modified":"2024-12-09 20:20:20"}],"ts":"1733672762","partnerid":"erp","method":"inventory.upload","sign":"352ba02ba39c1a5c387a0a7d45cdc81b"}';

        // $request = json_decode($request, 1);

        $msg = '';
        $res = Ecshop::updateInventory($request, $msg);
        // dd('sss',$res, $msg);
        if (!$res)
        {
            $result = ['code' => 500, 'msg' => $msg];
        }
        else
        {
            $result = ['code' => 0, 'msg' => '执行成功'];
        }

        return json_encode($result);
    }

    /**
     * @api {get} /shop/confirm_delivery
     * 接收：确认收货
     */
    public function confirm_delivery(Request $request)
    {
        $request = $request->all();

        // confirm_delivery_request:{"action_name":"\u786e\u8ba4\u6536\u8d27","as_id":1,"o_id":"test","outer_as_id":"test","remark":"test","shop_id":17292311,"so_id":"test","unique_id":"a500be35-c9c8-4703-a5bb-40aac288c6ff","items":[{"qty":1,"sku_id":"test"}],"ts":"1733672762","partnerid":"erp","method":"refund.goods","sign":"2bc2b67934513a24eef4ca8d7eb50726"}
        Log::info('confirm_delivery_request:'. json_encode($request));

        $msg = '';
        $res = Ecshop::confirmDelivery($request, $msg);
        // dd('sss',$res, $msg);
        if (!$res)
        {
            $result = ['code' => 500, 'msg' => $msg];
        }
        else
        {
            $result = ['code' => 0, 'msg' => '执行成功'];
        }

        return json_encode($result);
    }

}
